package com.bonc.ioc.controller;

import com.alibaba.fastjson.JSON;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.validation.AppAuthAnnotation;
import com.bonc.ioc.vo.ShareVo;
import com.bonc.ioc.service.impl.ShareServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分享相关接口
 * @author ZhangXu
 * @date 2021/3/18 16:51
 */
@RestController
@RequestMapping("/share")
public class ShareController {
    private static final Logger log = LoggerFactory.getLogger(ShareController.class);

    @Autowired
    private ShareServiceImpl shareService;

    /**
     * 添加分享接口
     * @param param 资源类型, 资源id, 用户id, 分享对象id, 分享开始时间, 分享结束时间, 备注
     * @return 添加结果
     */
    @PostMapping("/addShare")
    @AppAuthAnnotation
    public AppReply addShare(@RequestBody ShareVo param) {
        log.info("分享 添加分享 接收参数={}", JSON.toJSONString(param));
        Integer count = shareService.addShare(param);
        if (count > 0) {
            return AppReply.success("添加成功");
        }
        return AppReply.error("添加失败");
    }

    /**
     * 分页查询分享列表接口
     * @param param 用户id, 分享类型1:分享给我;2:我的分享, 查询关键字, 查询时段-开始时间, 查询时段-结束时间
     * @return 分享记录分页列表
     * @throws Exception
     */
    @PostMapping("/queryShare")
    public AppReply queryShare(@RequestBody ShareVo param) throws Exception {
        log.info("分享 获取分享列表 接收参数={}", JSON.toJSONString(param));
        PageResult result = shareService.queryShare(param);
        return new AppReply(AppReply.SUCCESS_CODE,"查询成功", result);
    }

    /**
     * 取消分享接口
     * @param param 分享id, 用户id
     * @return 取消结果
     */
    @PostMapping("/deleteShare")
    @AppAuthAnnotation
    public AppReply deleteShare(@RequestBody ShareVo param) {
        log.info("分享 取消分享 接收参数={}", JSON.toJSONString(param));
        Integer count = shareService.deleteShare(param);
        if(count > 0){
            return AppReply.success("取消成功");
        }
        return  AppReply.error("取消失败");
    }
}
